package com.tofine.smarthome.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.activeandroid.query.Select;
import com.tofine.smarthome.R;
import com.tofine.smarthome.adapter.DeviceAddToRoomAdapter;
import com.tofine.smarthome.base.BaseActivity;
import com.tofine.smarthome.model.Room;
import com.tofine.smarthome.util.DividerItemDecoration;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class DeviceAddToRoomActivity extends BaseActivity {

    @Bind(R.id.room_list) RecyclerView mRoomList;
    private DeviceAddToRoomAdapter mAdapter;
    private List<Room> mRoomData;
    @Bind(R.id.btn_add_device) AppCompatButton mBtnAdd;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_add_to_room;
    }

    @Override
    protected void initActionBar(android.support.v7.app.ActionBar actionBar) {
        super.initActionBar(actionBar);
        actionBar.getCustomView().findViewById(R.id.btn_add_device).setVisibility(View.INVISIBLE);
    }

    @Override
    protected int getActionBarTitle() {
        return R.string.title_activity_device_add_to_room;
    }

    @Override
    public void initView() {
        mRoomData = new Select().from(Room.class).execute();
        mAdapter = new DeviceAddToRoomAdapter(this, mRoomData);
        mRoomList.setLayoutManager(new LinearLayoutManager(this));
        mRoomList.addItemDecoration(new DividerItemDecoration(this, null));
        mRoomList.setAdapter(mAdapter);
    }

    @Override
    public void initData() {
    }

    @OnClick(R.id.btn_add_device)
    public void onClick() {
        int selection = mAdapter.getSelection();
        if (selection != -1) {
            Room room = mRoomData.get(mAdapter.getSelection());
            Bundle bundle = new Bundle();
            bundle.putString("new_device_room_name", room.name);
            Intent intent = new Intent();
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        } else {
            Toast.makeText(DeviceAddToRoomActivity.this, "请选择一个房间", Toast.LENGTH_SHORT).show();
        }
    }

}
